import schedule

from db.redisdb import RedisQueue
from db.mysqldb import MysqlDB
from utils.dd_message import message
from utils.log import logger

sql_conn = MysqlDB()
test = RedisQueue()
conn = test.connect()
import time


def send_msg():
    flag = ''

    try:
        if conn.ping():
            flag = True
            logger.info('200')
            conn.close()
    except Exception:
        flag = False
    if not flag:
        message('无法连接redis服务')


def send_status():

    try:
        all = sql_conn.find("select count(1) from __temp_GeneExpression_detailurl")[0][0]
        print(all)
        over = sql_conn.find("select count(1) from __temp_GeneExpression_detailurl where `status` =1")[0][0]
        print(over)
        currnet_count = conn.scard("biocompare:finger")
        done_count = conn.llen("productUrl")
        conn.close()
        desc = f"""
biocompare

列表页
待爬取总数:{currnet_count+done_count}
已爬取数量：{currnet_count}
待爬取数量:{done_count}
完成进度:{round(currnet_count / (currnet_count + done_count)*100,3)}%

详情页
GeneExpression
待爬取总数:{all}
已爬取数量:{over}
待爬取数量:{all-over}
完成进度：{round(over/all*100,3)}%
    """
        message(desc)
        # print(desc)
    except:
        pass




send_status()

schedule.every(20).seconds.do(send_msg)
schedule.every(3600).seconds.do(send_status)

while True:
    time.sleep(0.01)
    schedule.run_pending()
